import re
from string import Template

from pyexpat.errors import messages

# string是 不可变对象
def usage():
    #类型转换
    int1=999
    print(str(int1))

    var1 = 'hello word 单引号'
    print(var1)

    var2 = "hello word 双引号"
    print(var2)

    print("字符串运算符 ---------------")
    var3 = "hello word "
    print(var3[2])
    print(var3[0:1])
    print(var3[:3])
    print(var3 * 2)
    print(var3 + '666')

    print("and in ---------------")
    print("hello" in var3)
    print("hello" not in var3)
    print(var3 in "hello Word 666")

    print("r and R and fr ---------------不转义直接使用原始字符串")
    print("c:\desktop\\asd\\asda")
    print(r"c:\desktop\\asd\\asda")
    name = "fuck"
    print(rf"c:\desktop\\asd\\{name}")


### 格式化
def format():
    print("旧式 格式化---------------")
    print('fuck u %s and day %d' % ('he', 10))

    print("custemFormat() 方法 格式化---------------")
    message1 = "i wate fuck {}'s daughter to the week{} on the week{}。"
    message1 = message1.format('old he', 1, 7)
    print(message1)

    print("3.6之后f-string 格式化---------------")
    name = "lao he"
    week1 = 1
    week2 = 7
    message3 = f"i wate fuck {name}'s daughter to the week{week1} on the week{week2}。"
    print(message3)

    print("string.Template 格式化---------------")
    temp = Template("i wate fuck $name 's daughter to the week$week1 on the week$week2。")
    message4 = temp.substitute(name='lao he', week1=1, week2=7)
    print(message4)


###三引号
def there():
    there = """
        这是一个三引号，咳嗽随便写。
    都能保持格式，，，啦啦啦啦。
     \\ \ /b /n  
    """
    pass
    print(there)


### 转义
def escape():
    # 转义 https://www.runoob.com/python/python-strings.html
    var1 = 'hello word \\ \' \" '
    print(var1)


### 内置函数
def function():
    # char count
    print("lao he is bitch, asshole is pika,fuck it".count("a",0,9))
    # find
    print("lao he is bitch, asshole is pika,fuck it".find("ass"))
    # 标题化 Lao He Is Bitch, Asshole Is Pika,Fuck It
    print("lao he is bitch, asshole is pika,fuck it".title())
    #大写
    print("lao he is bitch, asshole is pika,fuck it".upper())
    #小写
    print("Lao He Is Bitch, Asshole Is Pika,Fuck It".lower())
    #大小写 翻转
    print("Lao He Is Bitch, Asshole Is Pika,Fuck It".swapcase())

    #截取 左空格
    print("截取-------")
    print("  Lao He Is Bitch".lstrip())
    print(", Asshole Is Pika,Fuck It   ".rstrip())
    print("  Lao He Is Bitch, Asshole Is Pika,Fuck It   ".strip())

    #分割
    print("分割-------")
    print("  Lao He Is Bitch, Asshole Is Pika,Fuck It   ".split(" ",6))

    #替换
    print("替换-------")
    #str.replace()
    print(str.replace("Lao He Is Bitch, Asshole Is Pika,Fuck It","Asshole","pussy").title())

    #str.translate() 高效单字符映射替换 只有一个参数maketrans
    # 单参数字典Key 必须为单字符  {old_char: new_char}
    # 两个参数 maketrans(old, new) old 和 new 必须是等长字符串，表示逐字符替换。
    # 三个参数 maketrans(old, new, delete)	替换 + 删除指定字符	maketrans("ab", "XY", "c") 而且删除的字符出现在 new中是无效的
    text="abcdef"
    table=str.maketrans({'a':"123","b":None})
    print(text.translate(text.maketrans(table)))
    print(text.translate(text.maketrans("ac","1.")))
    print(text.translate(text.maketrans("ac","1.","a")))




    pass

#正则
def reg():
    """
    re.sub(pattern, repl, string, count=0, flags=0)
    pattern: 要匹配的正则表达式模式
    repl: 替换字符串或替换函数
    string: 要在其中搜索的原始字符串
    count: 最大替换次数（默认为0，表示替换所有匹配）
    flags: 正则表达式标志（如 re.IGNORECASE）
    """
    res = 'Lao He Is Bitch, Asshole Is Pink,Fuck It'
    print(re.sub(r"Lao He", "xia", res))
    # 限制替换次数 忽略大小写
    print(re.sub(r"Is", "iss", res, 1, re.IGNORECASE))
    pass


#字符串追加
def append():
    #+ 或者+=
    user_str='user'
    print(user_str+'1')
    user_str += '2'
    print(user_str)

    user_str = 'user'
    append_list=[1,2,3,4,5]
    print(user_str.join([str(x) for x in append_list]))
    pass


if __name__ == '__main__':
    # usage()
    # custemFormat()
    # there()
    # escape()
    # function()
    # re
    append()
